package com.esri.core.internal.catalog;

import com.esri.core.internal.catalog.PageableResultType;
import com.esri.core.internal.io.handler.h;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonToken;

/* loaded from: classes.dex */
public class ResultCursor<T extends PageableResultType> implements Enumeration<T> {
    String f;
    HashMap<String, String> g;
    Class<T> h;
    Filter i;
    Catalog k;
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    int e = 0;
    List<T> j = new ArrayList();

    public void close() {
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public ResultCursor(Catalog catalog, String str, HashMap<String, String> hashMap, JsonParser jsonParser, Class<T> cls, Filter filter) {
        this.f = "";
        this.g = null;
        this.h = null;
        this.i = null;
        this.h = cls;
        this.i = filter;
        this.f = str;
        this.g = hashMap;
        this.k = catalog;
        init(jsonParser);
    }

    /* JADX WARN: Multi-variable type inference failed */
    protected void init(JsonParser jsonParser) {
        try {
            jsonParser.enable(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS);
            while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
                String currentName = jsonParser.getCurrentName();
                jsonParser.nextToken();
                if (currentName.equals("total")) {
                    this.a = jsonParser.getIntValue();
                } else if (currentName.equals("start")) {
                    this.b = jsonParser.getIntValue();
                } else if (currentName.equals("num")) {
                    this.c = jsonParser.getIntValue();
                } else if (currentName.equals("nextStart")) {
                    this.d = jsonParser.getIntValue();
                } else if (currentName.equals("results") || currentName.equals("relatedItems")) {
                    if (jsonParser.getCurrentToken() != JsonToken.START_ARRAY) {
                        throw new RuntimeException("Invalid Json response..");
                    }
                    while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
                        T newInstance = this.h.newInstance();
                        if (newInstance.fromJson(jsonParser, this.i, this.k)) {
                            this.j.add(newInstance);
                        }
                    }
                }
            }
            if (this.b == 0 && this.c == 0 && this.d == 0) {
                this.b = 0;
                this.a = this.j.size();
                PriorityQueue priorityQueue = new PriorityQueue(this.a, new Comparator<T>() { // from class: com.esri.core.internal.catalog.ResultCursor.1
                    @Override // java.util.Comparator
                    public int compare(T t, T t2) {
                        return t.compare(t2);
                    }
                });
                for (T t : this.j) {
                    priorityQueue.add(t);
                }
                for (int size = this.j.size() - 1; size >= 0; size--) {
                    this.j.remove(size);
                    this.j.add(size, priorityQueue.remove());
                }
                this.c = this.a;
                this.d = -1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getCount() {
        return this.a;
    }

    public void reset() {
        this.e = 0;
    }

    @Override // java.util.Enumeration
    public boolean hasMoreElements() {
        return this.e != this.a;
    }

    @Override // java.util.Enumeration
    public T nextElement() {
        int i = this.e;
        if (i == this.a) {
            throw new NoSuchElementException();
        }
        if (i == this.d - 1) {
            a();
        }
        List<T> list = this.j;
        int i2 = this.e;
        this.e = i2 + 1;
        return list.get(i2);
    }

    public List<T> nextTenElements() {
        if (this.e == this.a) {
            throw new NoSuchElementException();
        }
        if (this.d != -1 && this.j.size() - this.e < 10) {
            a();
        }
        int i = this.e;
        int i2 = i + 10;
        int i3 = this.a;
        if (i2 <= i3) {
            List<T> subList = this.j.subList(i, i + 10);
            this.e += 10;
            return subList;
        }
        List<T> subList2 = this.j.subList(i, i3);
        this.e = this.a;
        return subList2;
    }

    public int available() {
        return this.j.size();
    }

    public void seek(int i) throws IndexOutOfBoundsException {
        if (i < 0 || i >= this.j.size()) {
            throw new IndexOutOfBoundsException("Cannot seek the cursor to the specified position " + i);
        }
        this.e = i;
    }

    public List<T> getElements(int i, int i2) {
        while (i2 > this.j.size() - 1 && this.j.size() < this.a) {
            a();
        }
        int i3 = this.a;
        if (i2 <= i3) {
            return this.j.subList(i, i2 + 1);
        }
        return this.j.subList(i, i3);
    }

    private void a() {
        try {
            HashMap<String, String> hashMap = this.g;
            hashMap.put("start", this.d + "");
            init(h.a(this.f, this.g));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
